// --------------------------------------------
// an example using CInfoMap/CTask/CWork classes
// --------------------------------------------
#include <iostream>
#include <iterator>
#include <vector>
#include <list>
#include <algorithm>
#include <fstream>
#include <string>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#include "CInfoMap.h"

int main(int argc, char *argv[])
{
	if(argc<2) {
		cout << "usage : " << argv[0] << " *.task" << endl;
		exit(-1);
	}

	CInfoMap *im = new CInfoMap(argv[1]);

	vector<CTask> task_pool;
	while(im->hasMoreTasks()) {
		CTask task = im->getNextTask();
		//cout << "#task : <<<" << task.getName() << ">>>" << endl;

		while(task.hasMoreWorks()) {
			CWork work = task.getNextWork();
			//cout << "@work : " << work.getName() << endl;
		}

		task_pool.push_back(task);
	}

	for(int i=0;i<task_pool.size();i++) {
		cout << endl;
		cout << "==============================================================" << endl;
		cout << "\t\t\tTask : <" << task_pool[i].getName() << ">" << endl;
		cout << "==============================================================" << endl;
		task_pool[i].start();

		if(task_pool[i].waits()<0) {
			cout << endl << "## " << task_pool[i].getName() << "(" << task_pool[i].get_pid() << ") was abnormally exited" << endl;
			exit(-1);
		}
		else cout << endl << "## " << task_pool[i].getName() << "(" << task_pool[i].get_pid() << ") was successfully executed" << endl;
	}

	/* when executing concurrently 
	for(int i=0;i<task_pool.size();i++) {
		task_pool[i].start();
	}

	for(int i=0;i<task_pool.size();i++) {
		if(task_pool[i].waits()<0) cout << task_pool[i].getName() << "(" << task_pool[i].get_pid() << ") was abnormally exited" << endl;
		else cout << task_pool[i].getName() << "(" << task_pool[i].get_pid() << ") was successfully executed" << endl;
	}
	*/

	delete im;
}
